//
//  HMStatusViewModel.swift
//  微博
//
//  Created by 郭鉴增 on 2016/12/10.
//  Copyright © 2016年 郭鉴增. All rights reserved.
//

import UIKit
//微博数据处理对应的ViewModel
class HMStatusViewModel: NSObject {

    //需要引用微博数据模型
    var status: HMStatus? {
        didSet {
            //为了安全起见
            guard let currentStatus = status else {
                return
            }
            //处理会员等级图片
            handlembrankImage(mbrank: currentStatus.user?.mbrank ?? 0)
            //处理认证类型等级图片
            handleVerifiedTypeImage(verifiedType: currentStatus.user?.mbrank ?? 0)
            
            retweetCountContent = handleCountContent(count: currentStatus.reposts_count, defaultTitle: "转发")
            commentCountContent = handleCountContent(count: currentStatus.comments_count, defaultTitle: "评论")
            unlikeCountContent = handleCountContent(count: currentStatus.attitudes_count, defaultTitle: "赞")
            //处理转发微博内容
            handleRetweetContent(status: currentStatus)
        }
    }
    
    //转发微博内容
    var retweetContent: String?
    
    //会员等级图片
    var mbrankImage: UIImage?
    //认证类型等级图片
    var verifiedTypeImage: UIImage?
    //转发数内容
    var retweetCountContent: String?
    //评论数内容
    var commentCountContent: String?
    //赞数内容
    var unlikeCountContent: String?
    
    //转发微博显示内容
    private func handleRetweetContent(status: HMStatus) {
        //判断是否有转发微博
        if status.retweeted_status != nil {
            //获取转发微博用户的昵称
            //获取转发微博内容
            if let name = status.retweeted_status?.user?.screen_name, let text = status.retweeted_status?.text {
                
                //代码执行到此,表示昵称和转发微博内容一定有值
                retweetContent = "@\(name): \(text)"
                
            }
            
            
            
        }
    }
    
    /*
     转发,评论,赞 大于0 是否大于一万,就显示1.x万
                等于0  显示原始文字
     */
    private func handleCountContent(count: Int, defaultTitle: String) -> String {
        if count > 0 {
            if count >= 10000 {
                //显示1.xx万
                let result = CGFloat(count / 1000) / 10
                //判断是否包含".0"
                let str = "\(result)万"
                if str.contains(".0"){
                    return str.replacingOccurrences(of: ".0", with: "")
                }else{
                    return str
                }
                
            } else {
                return "\(count)"
            }
            
        } else {
            //显示原始文字
            return defaultTitle
        }
    }
    
    
    
    
    //处理会员等级图片
    private func handlembrankImage(mbrank: Int) {
        if mbrank >= 1 && mbrank <= 6 {
            mbrankImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        }
    }
    
    //处理认证类型等级图片
    private func handleVerifiedTypeImage(verifiedType: Int) {
        switch verifiedType {
        case 1:
            verifiedTypeImage = UIImage(named: "avatar_vip")
        case 2,3,5:
            verifiedTypeImage = UIImage(named: "avatar_enterprise_vip")
        case 220:
            verifiedTypeImage = UIImage(named: "avatar_grassroot")
        default:
            verifiedTypeImage = nil
        }
    }
    
    
    
    
}
